import json

from flask import Blueprint, request
from flask_restful import Resource, Api

# 编辑功能
import DcUrls

Edit = Blueprint('edit', __name__)
api = Api(Edit)


class edit(Resource):
    def get(self):
        dcContents = {}
        dcContents["key"] = "aaabbbccc"
        dcContents["data"] = setData()
        res = setResult(dcContents)
        return res

    def post(self):
        reqJson = json.loads(request.data)
        print(reqJson)
        dcContents = {}
        dcContents["key"] = "aaabbbccc"
        dcContents["data"] = setData()
        res = setResult(dcContents)
        return res


# 新增提交
class editCommit(Resource):
    def get(self):
        dcContents = {}
        dcContents["key"] = "aaabbbccc"
        res = setResult(dcContents)
        return res

    def post(self):
        reqJson = json.loads(request.data)
        print(reqJson)
        res = setResult("新增成功！")
        return res


api.add_resource(edit, '/edit')
api.add_resource(editCommit, '/editCommit')


def setData():
    data = []
    options = []
    dcContent = setViewConten("headImage", "头像", DcUrls.Url + "/static/images/title.png", "image", True, options)
    data.append(dcContent)
    options = []
    dcContent = setViewConten("userName", "姓名", "张三", "string", False, options)
    data.append(dcContent)
    options = [setDcOption("0", "男", ""), setDcOption("1", "女", "")]
    dcContent = setViewConten("userSex", "性别", "0", "radio", True, options)
    data.append(dcContent)

    options = [setDcOption("chess", "象棋", ""), setDcOption("cards", "打牌", ""), setDcOption("swimming", "游泳", ""),
               setDcOption("football", "足球", "")]
    dcContent = setViewConten("hobby", "爱好", "cards|swimming", "checkbox", True, options)
    data.append(dcContent)

    options = [setDcOption("ceo", "首席执行官", ""), setDcOption("cfo", "首席财政官", ""), setDcOption("coo", "首席运营官", "")]
    dcContent = setViewConten("post", "职业", "coo", "select", True, options)
    data.append(dcContent)

    options = []
    dcContent = setViewConten("birderDay", "生日", "2020-01-22", "date", True, options)
    data.append(dcContent)

    options = []
    dcContent = setViewConten("join", "加入时间", "2020-01-22 23:12:00", "datetime", True, options)
    data.append(dcContent)

    options = []
    dcContent = setViewConten("joinTime", "加入时间点", "23:12:00", "time", True, options)
    data.append(dcContent)
    return data


def setViewConten(key, text, value, Type, isOption, dcoption):
    dcConten = {}
    dcConten["key"] = key
    dcConten["text"] = text
    dcConten["value"] = value
    dcConten["type"] = Type
    dcConten["isOption"] = isOption
    dcConten["dc_option"] = dcoption
    return dcConten


def setDcOption(key, text, value):
    dcOption = {}
    dcOption["key"] = key
    dcOption["text"] = text
    dcOption["value"] = value
    return dcOption


def setResult(data):
    """
    通用返回数据格式
    :param data: 详细数据
    :return: result
    """
    result = {"code": 200, "message": "获取配置成功", "data": data}
    return result
